package com.dingreading.cloud.admin.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.admin.entity.StoreProtect;
import com.dingreading.cloud.admin.entity.Stores;
import com.dingreading.cloud.admin.entity.table.StoreProtectTableDef;
import com.dingreading.cloud.admin.mapper.StoreProtectMapper;
import com.dingreading.cloud.admin.service.StoreProtectService;
import com.dingreading.cloud.admin.service.StoresService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.NepUtil;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-11-21
 */
@Service
public class StoreProtectServiceImpl extends BaseServiceImpl<StoreProtectMapper, StoreProtect, StoreProtectTableDef> implements StoreProtectService {

    @Override
    protected StoreProtectTableDef getTable() {
        return StoreProtectTableDef.storeProtect;
    }

    @Resource
    private StoresService storesService;


    @Override
    public void deleteByStoreUid(String storeUid) {
        getMapper().deleteByCondition(table.storeUid.eq(storeUid));
    }

    @Override
    public List<StoreProtect> getByStoreUid(String storeUid) {
        return getMapper().selectListByCondition(table.storeUid.eq(storeUid));
    }

    @Override
    public List<StoreProtect> listByStoreUid(List<String> storeUids) {
        return getMapper().selectListByCondition(table.storeUid.in(storeUids));
    }

    @Override
    public List<String> getPolygonStoreList(String longitudeMin, String longitudeMax, String latitudeMin, String latitudeMax) {
        QueryCondition condition = table.lat.between(latitudeMin, latitudeMax)
                .and(table.lng.between(longitudeMin, longitudeMax))
                .and(table.enabled.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.storeUid)
                .from(table)
                .where(condition)
                .groupBy(table.storeUid);
        return getMapper().selectObjectListByQueryAs(wrapper, String.class);
    }

    @Override
    public void updateEnabled(String storeUid, Integer enabled) {
        UpdateChain.of(StoreProtect.class)
                .set(table.enabled, enabled)
                .where(table.storeUid.eq(storeUid))
                .update();
    }

    @Override
    public R<Object> getAllStoreProtects() {
        // 获取全部设置保护范围的门店
        List<Stores> storeList = storesService.getProtectStoreList();
        if (CollectionUtils.isEmpty(storeList))
            return R.ok();

        Map<Integer, List<Stores>> storeMap = storeList.stream().collect(Collectors.groupingBy(Stores::getProtectType));
        JSONObject jsonObject = new JSONObject();
        // 圆形保护范围
        List<Stores> circleStoreList = storeMap.get(1);
        if (CollectionUtils.isNotEmpty(circleStoreList)) {
            JSONArray circleList = new JSONArray();
            for (Stores data : circleStoreList) {
                JSONObject object = new JSONObject();
                object.put("storeName", data.getStoreName());
                object.put("storeUid", data.getUid());
                object.put("enabled", data.getEnabled());
                object.put("lng", data.getLng());
                object.put("lat", data.getLat());
                object.put("protectType", data.getProtectType());
                object.put("protectRange", NepUtil.nullToZero(data.getProtectRadius()));
                circleList.add(object);
            }
            jsonObject.put("circleList", circleList);
        }

        // 区域保护范围
        List<Stores> polygonStoreList = storeMap.get(2);
        if (CollectionUtils.isNotEmpty(polygonStoreList)) {
            List<String> polygonStoreUidList = polygonStoreList.stream().map(Stores::getUid).collect(Collectors.toList());
            // 获取区域保护的门店的经纬度
            List<StoreProtect> protectList = getMapper().selectListByCondition(table.storeUid.in(polygonStoreUidList));
            Map<String, List<StoreProtect>> maps = new HashMap<>();
            if (CollectionUtils.isNotEmpty(protectList))
                maps = protectList.stream().collect(Collectors.groupingBy(StoreProtect::getStoreUid));

            JSONArray polygonList = new JSONArray();
            for (Stores data : polygonStoreList) {
                List<StoreProtect> protects = maps.get(data.getUid());
                List<JSONObject> lngLatList = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(protects)) {
                    for (StoreProtect protect : protects) {
                        JSONObject object = new JSONObject();
                        object.put("lng", protect.getLng());
                        object.put("lat", protect.getLat());
                        lngLatList.add(object);
                    }
                }

                JSONObject object = new JSONObject();
                object.put("storeName", data.getStoreName());
                object.put("storeUid", data.getUid());
                object.put("enabled", data.getEnabled());
                object.put("lng", data.getLng());
                object.put("lat", data.getLat());
                object.put("protectType", data.getProtectType());
                object.put("list", lngLatList);
                polygonList.add(object);
            }
            jsonObject.put("polygonList", polygonList);
        }
        return R.ok(jsonObject);
    }

}
